<?php
// get name of request
if(!$_GET['show'] == "") {
  $req = $_GET['show'];
}
if(!$_GET['edit'] == "") {
  $req = $_GET['edit'];
}
// normalize the title for later use (caps, etc)
$title = sqlite_fetch_single(sqlite_query($db,"SELECT title FROM pages WHERE title LIKE '$req'"));
if(!$title) {
  $title = $req;
}
// flow (needs getPage() function!)
if(!$_GET['show'] == "") {
  if($_GET['show'] == "_index") {
    // list all pages
    $doctitle = "/pages";
    $header = $doctitle;
    $content = "listpages.php";
    $pages = sqlite_fetch_all(sqlite_query($db,"SELECT * FROM pages"));
  } else {
    // show page
    $page = sqlite_fetch_array(sqlite_query($db,"SELECT * FROM pages WHERE title LIKE '$title'"));
	  $doctitle = "/pages/$title";
	  $header = "/<a href='?show=_index'>pages</a>/$title";
    if($page) {
      $revisions = sqlite_fetch_all(sqlite_query($db,"SELECT * FROM revisions WHERE pageid=".$page['id']." ORDER BY time"));
      // get pages that link to this
      $linkedtoby = array();
      $fromids = sqlite_fetch_all(sqlite_query($db,"SELECT fromid FROM links WHERE to LIKE '$title'"));
      foreach($fromids as $link) {
        $linkid = $link['fromid'];
        array_push($linkedtoby,sqlite_fetch_single(sqlite_query($db,"SELECT title FROM pages WHERE id = $linkid")));
      }
      // which revision?
      if($_GET['revision'] == "") {
        // latest
        $displayrevisionid = count($revisions);
      } else {
        // certain revision
        $doctitle .= "/".$_GET['revision'];
        $header = "/<a href='?show=_index'>pages</a>/<a href='?show=$title'>$title</a>/".$_GET['revision'];
        $displayrevisionid = $_GET['revision'];
      }
      $displayrevision = $revisions[$displayrevisionid-1];
      $content = "show.php";
    } else {
      $content = "notfound.php";
      // search
      $pages = sqlite_fetch_all(sqlite_query($db,"SELECT * FROM pages WHERE title LIKE '%$title%'"));
    }
  }
} else if(!$_GET['edit'] == "") {
  // edit page
  $doctitle = "/pages/$title/edit";
  $header = "/<a href='?show=_index'>pages</a>/<a href='?show=$title'>$title</a>/edit";
  $content = "edit.php";
  if($page = sqlite_fetch_array(sqlite_query($db,"SELECT * FROM pages WHERE title LIKE '$title'"))) {
    $query = "SELECT * FROM revisions WHERE pageid=".$page['id']." ORDER BY time DESC";
    $revision = sqlite_fetch_array(sqlite_query($db,$query));
  }
} else if(!$_GET['tag'] == "") {
  $tag = $_GET['tag'];
  if($tag == "_index") {
    // list tags
    $doctitle = "/tags";
    $header = $doctitle;
    $content = "listtags.php";
  } else {
    // list pages tagged something
    $doctitle = "/tags/$tag";
    $header = "/<a href='?tag=_index'>tags</a>/$tag";
    $content = "listpages.php";
    $pages = sqlite_fetch_all(sqlite_query($db,"SELECT * FROM pages WHERE tags LIKE '%$tag%'"));
  }
} else if(!$_GET['meta'] == "") {
  $content = "special/meta/".$_GET['meta'].".php";
  $doctitle = "/meta/".$_GET['meta'];
  $header = $doctitle;
} else if(!$_GET['special'] == "") {
  $content = "special/".$_GET['special'].".php";
  $doctitle = "/special/".$_GET['special'];
  $header = $doctitle;
} else {
  // main page
  header("location: ".$config['url']."?show=main");
}
?>